/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.TextStyle;
import java.util.Date;
import java.util.Locale;

/**
 * 时间日志工具类
 *
 * @auther ebert_chan
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(DateUtils.class);

    /**
     * format: yyyy-MM-dd HH:mm:ss
     */
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * format: yyyy-MM-dd
     */
    public static final String DATE_FORMAT = "yyyy-MM-dd";

    /**
     * 当前时间
     *
     * @return
     * @auther ebert_chan
     */
    public static Date now() {
        LocalDateTime now = LocalDateTime.now();
        Date date = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
        return date;
    }

    /**
     * 当前时间的字符串形式
     *
     * @return
     * @auther ebert_chan
     */
    public static String nowTimeString() {
        return formatDateOfNormal(now());
    }

    /**
     * Date转LocalDateTime
     *
     * @param date
     * @return
     * @auther ebert_chan
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        return localDateTime;
    }

    /**
     * Date格式化为字符串， 常规格式：yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     * @auther ebert_chan
     */
    public static String formatDateOfNormal(Date date) {
        return formatDate(date, DATETIME_FORMAT);
    }

    /**
     * Date格式化为字符串
     *
     * @param date
     * @param pattern
     * @return
     * @auther ebert_chan
     */
    public static String formatDate(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 字符串解析为Date， 常规格式：yyyy-MM-dd HH:mm:ss
     *
     * @param dateString
     * @return
     * @auther ebert_chan
     */
    public static Date parseDateOfNormal(String dateString) {
        return parseDate(dateString, DATETIME_FORMAT);
    }

    /**
     * 字符串解析为Date
     *
     * @param dateString
     * @param pattern
     * @return
     * @auther ebert_chan
     */
    public static Date parseDate(String dateString, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(dateString);
        } catch (ParseException e) {
            LOGGER.warn("An exception occurred.", e);
        }
        return null;
    }

    /**
     * 获取指定日期是周几的函数，比如：一、二
     *
     * @param date
     * @return
     * @auther ebert_chan
     */
    public static String getChineseDayOfWeek(Date date) {
        LocalDateTime localDateTime = dateToLocalDateTime(date);
        return localDateTime.getDayOfWeek().getDisplayName(TextStyle.NARROW, Locale.CHINESE);
    }

}
